Introduction
We are going to reproduce three different plots, of which we are only given the data set and the final image file. Each of them has two versions:
- The first version is a plot made just using the layer functions of
ggplot2and leaving everything else with the defaults. This is, usingggplot,geom_*,stat_*andannotate_*functions (click on the image to see it bigger).
- The second version builds from the first plot to create a more
complex display with customized
scales_*,coord_*,facet_*ortheme_*. Helper functions such aslabs()orguides()could also be used (click on the image to see it bigger).
: ggplot2 will
automatically load all three data sets used in this practical.
Organization of the practical
You will see different icons through the document, the meaning of which is:
: additional or useful
information
: a worked example
: a practical exercise
: a space to answer the exercise
: a hint to solve an exercise
: a more challenging exercise
Simple versions
Plot 1
Plot 2
Need help with the count numbers shown in each stacked bar? https://stackoverflow.com/questions/6644997/showing-data-values-on-stacked-bar-chart-in-ggplot2
Answer:
# Dataset used: mpg
ggplot(data=mpg, mapping = aes(x=manufacturer, fill = class))+
geom_bar()+
geom_text(stat='count', aes(label=..count..), position = position_stack(vjust = 0.5))## Warning: The dot-dot notation (`..count..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(count)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
Complex versions
Plot 1
Plot 2
Answer:
ggplot(data=mpg, aes(x=manufacturer, fill = class))+
geom_bar()+
geom_text(stat='count', aes(label=..count..), position = position_stack(vjust = 0.5))+
coord_flip()+scale_fill_brewer(type="qual", palette = 2)+
labs(x="Manufacturer", y="Number of cars", fill = "Car type")
This graph uses a
scale_fill_brewer palette, can you find which one?
Plot 3
Answer:
ggplot(data=ToothGrowth, aes(x=as.factor(dose), y=len))+
geom_boxplot(aes(colour = supp, fill = supp), color="black")+
geom_hline(data= ToothGrowth, yintercept= mean(ToothGrowth$len), lty = "dashed")+
scale_fill_brewer(type="seq", palette=7, direction=-1, label= c("Orange juice", "Vitamin C"))+
labs(y="Tooth length", x="Dose (mg/day)", fill="Supplement")+
theme_classic()
This graph uses a
scale_fill_brewer palette, can you find which one?